import { useRef, useState, useEffect } from "react";
export function useThrottle<T>(value: T, delay: number): T {
  const [throttledValue, setThrottledValue] = useState(value);
  const lastRan = useRef(Date.now());
  useEffect(() => {
    if (Date.now() - lastRan.current >= delay) {
      setThrottledValue(value);
      lastRan.current = Date.now();
    } else {
      const timer = setTimeout(() => {
        setThrottledValue(value);
        lastRan.current = Date.now();
      }, delay - (Date.now() - lastRan.current));
      return () => clearTimeout(timer);
    }
  }, [value, delay]);
  return throttledValue;
}